<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="部门状态" clearable style="width: 200px">
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked"  @Launchform="Launchform3"/>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="Sort" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  
      v-if="refreshTable"  
      v-loading="loading"  
      :data="getClassifiedDataDefinitionListMethodsTableDate"  
      row-key="typeId"  
      :default-expand-all="isExpandAll"  
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"  
    >  
      <el-table-column   
        prop="typeName"   
        label="材料名称"   
        width="200"   
        :class-name="getTypeNameClass"  
      ></el-table-column>  

      <el-table-column   
        prop="orderNum"   
        label="排序"   
        width="200"   
        :class-name="getTypeNameClass"  
      ></el-table-column>  

      <el-table-column   
        prop="typeNo"   
        label="编码"   
        width="200"   
        :class-name="getTypeNameClass"  
      ></el-table-column>  

      <el-table-column   
        prop="remark"   
        label="备注"   
        width="200"   
        :class-name="getTypeNameClass"  
      ></el-table-column>  
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">                                                  
        <template #default="scope">  
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dept:edit']">修改</el-button>  
          <el-button link type="primary" icon="Plus" @click="handleAdd1(scope.row)" v-hasPermi="['system:dept: add']">新增</el-button>  
          <el-button  
            v-if="scope.row.parentId != 0"  
            link  
            type="primary"  
            icon="Delete"  
            @click="handleCloseMateriaLKlc9(scope.row)"  
            v-hasPermi="['system:dept:remove']"  
            >删除</el-button  
          >  
        </template>  
      </el-table-column>  
    </el-table>  

    <!-- 添加或修改部门对话框 -->
    <el-dialog :close-on-click-modal="false" title="修改数据" v-model="open" width="600px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="材料名称">
        <el-input v-model="FitProcessingMethodUpdateParms.typeName" placeholder="请输入材料名称" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="FitProcessingMethodUpdateParms.orderNum" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="FitProcessingMethodUpdateParms.typeNo" placeholder="请输入材编码" />
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="FitProcessingMethodUpdateParms.remark" placeholder="请输入备注" />
      </el-form-item>
      <!-- <el-form-item label="领用单位">
        <el-input v-model="FitProcessingMethodUpdateParms.prdType" placeholder="请输入加工方式" />
      </el-form-item> -->


      <el-form-item label="主辅料" class="f14">
              <el-select v-model="FitProcessingMethodUpdateParms.mtlProperty" placeholder="主辅料" >
                <el-option v-for="item in DepartmentType" :key="item.id" :label="item.name" :value="item.name" />
              </el-select>
            </el-form-item>


      <el-form-item label="领用单位">
        <el-select
                style="width: 300px" 
                v-model="FitProcessingMethodUpdateParms.drawDept" 
                placeholder="领用单位"  
              >
                <el-option v-for="item in AccountingType" :key="item.id" :label="item.name" :value="item.name" />
                </el-select>
      </el-form-item>
      

    </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm1">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>


        <!-- 添加或修改部门对话框 -->
    <el-dialog :close-on-click-modal="false" title="添加数据" v-model="open1" width="600px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="材料名称">
        <el-input v-model="FitProcessingMethodAddParms.typeName" placeholder="请输入材料名称" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="FitProcessingMethodAddParms.orderNum" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="FitProcessingMethodAddParms.typeNo" placeholder="请输入编码" />
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="FitProcessingMethodAddParms.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="主辅料" class="f14">
              <el-select v-model="FitProcessingMethodAddParms.mtlProperty" placeholder="主辅料" >
                <el-option v-for="item in DepartmentType" :key="item.id" :label="item.name" :value="item.name" />
              </el-select>
            </el-form-item>
      <el-form-item label="领用单位">
        <el-select
                style="width: 300px"
                v-model="FitProcessingMethodAddParms.drawDept"
                placeholder="领用单位"
              >
                <el-option v-for="item in AccountingType" :key="item.id" :label="item.name" :value="item.name" />
                </el-select>
      </el-form-item>
    </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm2">确 定</el-button>
          <el-button @click="open1 = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RdTypeBodyFileList">
import SelectUserInput from '@/components/SelectUserInput'
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from '@/api/system/dept'
import {getClassifiedDataDefinitionList ,delClassifiedDataDefinition,updateClassifiedDataDefinition,addClassifiedDataDefinition} from '@/api/ControlDepartment/MaterialManagement/ClassifiedDataDefinition'
import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'
import {cloneDeep} from 'lodash'


// 部门类型
const DepartmentType = ref([
  { id: 'P', name: '冲裁主料' },
  { id: 'M', name: '冲裁辅料' },
  { id: 'J', name: '针车辅料' },
  { id: 'Z', name: '' }
])

// 核算类型
const AccountingType = ref([
  { id: 'Y', name: '冲裁' },
  { id: 'N', name: '外协' },
  { id: 'J', name: '电脑车' },
  { id: 'D', name: '针车' },
  { id: 'A', name: '成型' },
  { id: 'Z', name: '' }
])

// 分类资料定义新增
const open1 = ref(false)

const FitProcessingMethodAddParms = ref({
  ancestors: "",
  
  
  
  params: {},
  

  remark: "",

  drawDept: "",
  typeId: 0,
  typeName: "",
  typeNo: "",
  parentId: 0,
  orderNum: 0,
  mtlProperty: "",
})

const handleAdd1 = (row) => {
  console.log(row)
  FitProcessingMethodAddParms.value.parentId = row.typeId
  open1.value = true
}

const submitForm2 = async() => {
  const result = await addClassifiedDataDefinition(FitProcessingMethodAddParms.value)
  if (result.code === 200) {
    ElMessage.success(result.msg)
    open1.value = false
    FitProcessingMethodAddParms.value.typeName = ""
    FitProcessingMethodAddParms.value.orderNum = ""
    FitProcessingMethodAddParms.value.typeNo = ""
    FitProcessingMethodAddParms.value.remark = ""
    FitProcessingMethodAddParms.value.prdType = ""
    FitProcessingMethodAddParms.value.mtlProperty = ""
    FitProcessingMethodAddParms.value.drawDept = ""
    getClassifiedDataDefinitionListMethods()
  } else{
    ElMessage.error(result.msg)
  }
}

/** 修改按钮操作 */
const  FitProcessingMethodUpdateParms = ref()
function handleUpdate(row) {
  open.value = true
  FitProcessingMethodUpdateParms.value = cloneDeep(row)
}

// 修改
const submitForm1 = async() => {
  const result = await updateClassifiedDataDefinition(FitProcessingMethodUpdateParms.value)
  if (result.code === 200) {
    ElMessage.success(result.msg)
    open.value = false
    getClassifiedDataDefinitionListMethods() 
  } else{
    ElMessage.error(result.msg)
  }
}

// 删除
const handleCloseMateriaLKlc9Parms = ref('')
const delClassifiedDataDefinitionMethods = async(row) => {
  const result = await delClassifiedDataDefinition(handleCloseMateriaLKlc9Parms.value)
  if (result.code === 200) {
    ElMessage.success(result.msg)
    getClassifiedDataDefinitionListMethods() 
  }else{
    ElMessage.error(result.msg)
  }
}

const handleCloseMateriaLKlc9 = (row) => {
  handleCloseMateriaLKlc9Parms.value = row.typeId
  ElMessageBox.confirm('确认要删除吗?')
    .then(async () => {
      delClassifiedDataDefinitionMethods()
    })
    .catch(() => {})
}

const getClassifiedDataDefinitionListMethodsTableDate = ref([])

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: '',
    inputLabel:'请输入材料名称'
  },
  {
    type: 'input',
    inputPlaceholder: '请输入编码',
    inputLabel:'请输入编码',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '搜索',
    bgColor: '#67C23A'
  },
])

const Launchform3 = (row) => {
  getClassifiedDataDefinitionListParms.value.typeName = row[0].inputValue
  getClassifiedDataDefinitionListParms.value.typeNo = row[1].inputValue
}

const findClicked = (row) => {
  if(row === '置空'){
    formList.value[0].inputValue = ''
    formList.value[1].inputValue = ''
    
  }
  if(row === '搜索'){
    getClassifiedDataDefinitionListMethods()
   }
}

const getClassifiedDataDefinitionListParms = ref({
  typeName: '',
  typeNo: ''
})

const getClassifiedDataDefinitionListMethods = async() => {
  loading.value = true
  
  const result = await getClassifiedDataDefinitionList(getClassifiedDataDefinitionListParms.value)
  getClassifiedDataDefinitionListMethodsTableDate.value = result.data
  loading.value = false
}

onMounted(() => {
  getClassifiedDataDefinitionListMethods()
})

const { proxy } = getCurrentInstance()
const { sys_normal_disable } = proxy.useDict('sys_normal_disable')

const deptList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const title = ref('')
const deptOptions = ref([])
const isExpandAll = ref(true)
const refreshTable = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    deptName: undefined,
    status: undefined
  },
  rules: {
    parentId: [{ required: true, message: '上级部门不能为空', trigger: 'blur' }],
    deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
    orderNum: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }],
    leader: [{ required: true, message: '负责人不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询部门列表 */
function getList() {


}
/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}
/** 表单重置 */
function reset() {
  form.value = {
    deptId: undefined,
    parentId: undefined,
    deptName: undefined,
    orderNum: 0,
    leader: undefined,
    status: '0'
  }
  proxy.resetForm('deptRef')
}
/** 搜索按钮操作 */
function handleQuery() {
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
/** 新增按钮操作 */
function handleAdd(row) {
  reset()
  listDept().then((response) => {
    deptOptions.value = proxy.handleTree(response.data, 'deptId')
  })
  if (row != undefined) {
    form.value.parentId = row.deptId
  }
  open.value = true
  title.value = '添加部门'
}
/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['deptRef'].validate((valid) => {
    if (valid) {
      if (form.value.deptId != undefined) {
        updateDept(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {admin
        addDept(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}
/** 删除按钮操作 */


// 选择负责人
function handleSelectUser(user) {}

getList()
</script>

<style>

</style>
